/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zouqone.smc.common.web;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zouqone.smc.common.utils.ActionHelp;
import com.zouqone.smc.common.utils.http.HttpClientUtil;
import com.zouqone.smc.common.utils.http.HttpClientUtil.MultipartForm;

/**
 * CKFinderConnectorServlet
 * @author ThinkGem
 * @version 2014-06-25
 */
public class CKFinderConnectorServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpClientUtil httpClient = new HttpClientUtil();
		MultipartForm form = httpClient.new MultipartForm();

		String url = "http://123.57.57.198:8080/smc/static/ckfinder/core/connector/java/connector.java";
	       
		form.setAction(url);
		File file = new File("/tmp/比对工具.txt");
		// 上传uploadDirectory目录下的所有文件
		String name = "file";
		form.addFileField(name, file);

		HttpClientUtil.submit(form);
		
		//doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String command = request.getParameter("command");
		
		System.out.println("command : "+command);
		DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setHeaderEncoding("utf-8");
        List<FileItem> fileList = null;
        try {
            fileList = upload.parseRequest(request);
        } catch (FileUploadException ex) {
            return;
        }
        Iterator<FileItem> it = fileList.iterator();
        String name = "";
        while (it.hasNext()) {
            FileItem item = it.next();
            if (!item.isFormField()) {
                name = item.getName();
                if (name == null || name.trim().equals("")) {
                    continue;
                }
                System.out.println("upload File name : "+name);
                ActionHelp.WriteStrToOut(response, "{\"success\":\"1\"}");
                break;
            }
        }
	}
	
	
}
